using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

using EntLibContrib.Validation.Validators;

namespace EntLibContrib.Validation.Tests.Validators
{
    [TestClass]
    public class EnumDefinedValidatorFixture
    {
        [TestMethod, DeploymentItem(@"Validators\EnumDefinedValidator.config")]
        public void EnumDefinedHasNoErrors()
        {
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create("EnumDefinedValidator");

            Foo foo = new Foo();

            Validator validator = ValidationFactory.CreateValidatorFromConfiguration(typeof(Foo), "Defined", configurationSource);

            ValidationResults results = validator.Validate(foo);

            Assert.IsTrue(results.IsValid);
        }

        [TestMethod, DeploymentItem(@"Validators\EnumDefinedValidator.config")]
        public void EnumNotDefinedHasErrors()
        {
            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create("EnumDefinedValidator");

            Foo foo = new Foo();

            Validator validator = ValidationFactory.CreateValidatorFromConfiguration(typeof(Foo), "NotDefined", configurationSource);

            ValidationResults results = validator.Validate(foo);

            Assert.IsFalse(results.IsValid);
        }

        [TestMethod]
        public void EnumDefinedWithAttributesHasNoErrors()
        {
            FooWithAttributes foo = new FooWithAttributes();

            Validator validator = ValidationFactory.CreateValidator(typeof(FooWithAttributes), "Defined");

            ValidationResults results = validator.Validate(foo);

            Assert.IsTrue(results.IsValid);
        }

        [TestMethod]
        public void EnumNotDefinedWithAttributesHasErrors()
        {
            FooWithAttributes foo = new FooWithAttributes();

            Validator validator = ValidationFactory.CreateValidator(typeof(FooWithAttributes), "NotDefined");

            ValidationResults results = validator.Validate(foo);

            Assert.IsFalse(results.IsValid);
        }

        public enum Bar
        {
            A = 0,
            B = 1
        }

        public class Foo
        {
            public Bar Defined
            {
                get { return Bar.A; }
            }

            public Bar NotDefined
            {
                get { return (Bar)2; }
            }
        }

        public class FooWithAttributes
        {
            [EnumDefinedValidator(Ruleset = "Defined")]
            public Bar Defined
            {
                get { return Bar.A; }
            }

            [EnumDefinedValidator(Ruleset = "NotDefined")]
            public Bar NotDefined
            {
                get { return (Bar)2; }
            }
        }
    }
}
